#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

//The data structure we use for passing more then one argument to the thread.
typedef struct
{
	int threadId;
	int totalNumberOfThreads;
} data_t;

//The function each thread runs.
void *PrintThread(void *threaddata)
{
	//makeing a pointer to a struct of type data.
	data_t *theData;
	//casting the data givin to the function to a struct of type data.
	theData = (data_t *) threaddata;
	int id;
	id = theData->threadId + 1;
	printf("thread %d/%d \n", id,theData->totalNumberOfThreads);
	pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
	int numThreads;
	numThreads = atoi(argv[1]);
	//printf("%d\n", numThreads); //To debug
	pthread_t threads[numThreads];
	data_t dataArray[numThreads];
	
	int currentThread;
	int i;
	for(i=0; i<numThreads; i++)
	{
		dataArray[i].threadId = i;
		dataArray[i].totalNumberOfThreads = numThreads;
		
		currentThread = pthread_create(&threads[i], NULL, PrintThread, (void *) &dataArray[i]);
		if (currentThread)
		{
			printf("Error: something didn't go the way it was supposed to! %d\n", currentThread);
			return(1);
		}
	}
	pthread_exit(NULL);
}

